//
// Created by 123 on 2025/1/13.
//
#include "iostream"
#include "vector"
#include "algorithm"

class MyPrint{
public:
    void operator()(int val){
        std::cout << val << " ";
    }
};

void test(){
    std::vector<int> v1;
    std::vector<int> v2;
    for (int i = 0; i < 10; ++i) {
        v1.push_back(i);
        v2.push_back(i+5);
    }
    std::vector<int> vTarget;

    //求交集
    //取两个最小的值给目标容器开辟空间
    vTarget.resize(std::min(v1.size(),v2.size()));
    //返回目标容器最后一个元素的迭代器地址
   std::vector<int>::iterator itEnd =  std::set_intersection(v1.begin(), v1.end(),v2.begin(), v2.end(),vTarget.begin());
   //此处不要用vtarget.end(_),因为实际可能没满
    std::for_each(vTarget.begin(),itEnd,MyPrint());
    std::cout << std::endl;
    std::for_each(vTarget.begin(),vTarget.end(),MyPrint());
    std::cout << std::endl;

    //求并集
    std::vector<int> vTarget2;
    vTarget2.resize(v1.size()+v2.size());
    std::vector<int>::iterator itEnd2 = std::set_union(v1.begin(), v1.end(),v2.begin(), v2.end(),vTarget2.begin());
    std::for_each(vTarget2.begin(),itEnd2,MyPrint());
    std::cout << std::endl;
    std::for_each(vTarget2.begin(),vTarget2.end(),MyPrint());
    std::cout << std::endl;

    //求差集
    std::vector<int> v3;
    v3.resize(std::max(v1.size(),v2.size()));
    std::vector<int>::iterator i3 = std::set_difference(v1.begin(), v1.end(),v2.begin(), v2.end(),v3.begin());
    std::for_each(v3.begin(),i3,MyPrint());
    std::cout << std::endl;
    std::for_each(v3.begin(), v3.end(),MyPrint());
    std::cout << std::endl;

}

int main(){
    test();
    return 0;
}